﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ParentChildLookup.Tools;
using System.Data;
using System.Collections;
using Microsoft.SharePoint.Utilities;
using ParentChildLookup.Data;

namespace ParentChildLookup.Pages
{
    public partial class SelectObject : LayoutsPageBase
    {
        protected override SPBasePermissions RightsRequired
        {
            get
            {
                return (SPBasePermissions.EmptyMask | SPBasePermissions.ManageLists);
            }
        }

        public string RedirectUrl
        {
            get
            {
                return "ParentChildLookup/ManageLinkedColumns.aspx";
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            ddlType.SelectedIndexChanged += new EventHandler(ddlType_SelectedIndexChanged);

            if (ddlType.Items.Count == 0)
            {
                ddlType.Items.Add(new ListItem(SPTools.GetParentChildLookupResource("Page_Type_ContentTypes", base.Web), "0"));
                ddlType.Items.Add(new ListItem(SPTools.GetParentChildLookupResource("Page_Type_Lists", base.Web), "1"));
            }

            if (!this.Page.IsPostBack)
            {
                this.UpdateListOfLists();
                this.UpdateListOfContentTypes();
            }


            lblType.Text = SPTools.GetParentChildLookupResource("Page_TypeLabel", base.Web);
            ltPageTitle.Text = SPTools.GetParentChildLookupResource("Page_ManageLinkedColumn", base.Web);
            ltPageTitleInTitle.Text = SPTools.GetParentChildLookupResource("Page_ManageLinkedColumn", base.Web);
            ltPageDescription.Text = SPTools.GetParentChildLookupResource("Page_ManageLinkedColumn_Desc", base.Web);
            ltListHeading.Text = SPTools.GetParentChildLookupResource("Page_ListHeading", base.Web);
            ltListDescription.Text = SPTools.GetParentChildLookupResource("Page_ListDescription", base.Web);
            ltListType.Text = SPTools.GetParentChildLookupResource("Page_ListType", base.Web);
        }


        void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            mvTypes.ActiveViewIndex = Convert.ToInt32(ddlType.SelectedValue);
        }

        private void UpdateListOfContentTypes()
        {
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("Title", typeof(string)));
            table.Columns.Add(new DataColumn("Href", typeof(string)));
            table.Columns.Add(new DataColumn("Group", typeof(string)));
            table.Columns.Add(new DataColumn("Source", typeof(string)));
            string serverRelativeUrl = base.Web.ServerRelativeUrl;
            if (!serverRelativeUrl.EndsWith("/"))
            {
                serverRelativeUrl = serverRelativeUrl + "/";
            }
            string str2 = serverRelativeUrl;
            serverRelativeUrl = serverRelativeUrl + "_layouts/" + RedirectUrl + "?ObjectType=" + SPData.Enumerations.ObjectType.ContentType.ToString() + "&ObjectId=";

            foreach (SPContentType current in base.Web.ContentTypes)
            {
                if (!current.Hidden && !current.ReadOnly && current.Group != "_Hidden")
                {
                    DataRow row = table.NewRow();
                    row["Title"] = SPHttpUtility.HtmlEncode(current.Name);
                    row["Group"] = current.Group;
                    row["Source"] = current.ParentWeb.Title;
                    row["Href"] = SPHttpUtility.HtmlUrlAttributeEncode(SPHttpUtility.UrlPathEncode(serverRelativeUrl + current.Id.ToString().ToUpper(), true));

                    table.Rows.Add(row);
                }
            }

            this.RptListOfContentTypes.DataSource = table;
            this.RptListOfContentTypes.DataBind();
            return;
        }

        private void UpdateListOfLists()
        {
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("Title", typeof(string)));
            table.Columns.Add(new DataColumn("Href", typeof(string)));
            table.Columns.Add(new DataColumn("Description", typeof(string)));
            table.Columns.Add(new DataColumn("ListType", typeof(string)));
            string serverRelativeUrl = base.Web.ServerRelativeUrl;
            if (!serverRelativeUrl.EndsWith("/"))
            {
                serverRelativeUrl = serverRelativeUrl + "/";
            }
            string str2 = serverRelativeUrl;
            serverRelativeUrl = serverRelativeUrl + "_layouts/" + RedirectUrl + "?ObjectType=" + SPData.Enumerations.ObjectType.List.ToString() + "&ObjectId=";

            foreach (SPList current in base.Web.Lists)
            {
                if (!current.Hidden)
                {
                    DataRow row = table.NewRow();
                    row["Title"] = SPHttpUtility.HtmlEncode(current.Title);
                    row["Description"] = current.Description;
                    row["ListType"] = current.BaseType.ToString();
                    row["Href"] = SPHttpUtility.HtmlUrlAttributeEncode(SPHttpUtility.UrlPathEncode(serverRelativeUrl + current.ID.ToString("B").ToUpper(), true));

                    table.Rows.Add(row);
                }
            }

            this.RptListOfLists.DataSource = table;
            this.RptListOfLists.DataBind();
            return;
        }
    }
}
